home *** CD-ROM | disk | FTP | other *** search
/ Mastering Computers 3 / Mastering Computers Vol 3.iso / Drivers / Printer drivers / HP / HPCLIP.EXE / HPCLIP.C next >
Encoding:
C/C++ Source or Header  |  1990-07-16  |  6.0 KB  |  248 lines

  1.  
  2.  
  3. /************************************************************************/
  4. /*            hpclip.c                    */
  5. /************************************************************************/
  6. #define NOCOMM
  7.  
  8. #include "windows.h"
  9. #include "hpclip.h"
  10. #include <stdio.h>
  11. #include <string.h>    /* for strtok */
  12.  
  13. /* Modules */
  14. int PASCAL WinMain(HANDLE,HANDLE,LPSTR,int);
  15. long FAR PASCAL DemoWndProc(HWND,unsigned,WORD,LONG);
  16. VOID DemoPaint(HWND);
  17. VOID Print(HWND);
  18. VOID DrawFilledArea(HWND,HDC);
  19. VOID FAR PASCAL ShadeXProc(short,short,LPSTR);
  20.  
  21. /* Globals */
  22. char szAppName[10] = "HPCLIP";
  23. char szMessage[60] = "Demonstrate Setting Cliping Region" ;
  24. HANDLE hInst;
  25. HANDLE hLibrary;
  26. HDC hShadeDC;
  27. POINT ptShadeLength;
  28.  
  29. /*************************************************************************/
  30. int PASCAL WinMain( hInstance, hPrevInstance, lpszCmdLine, cmdShow )
  31. HANDLE hInstance, hPrevInstance;
  32. LPSTR lpszCmdLine;
  33. int cmdShow;
  34. {
  35. MSG   msg;
  36. HWND  hWnd;
  37. WNDCLASS wndclass;
  38.  
  39. if (!hPrevInstance)
  40.    {
  41.    wndclass.style          = CS_HREDRAW | CS_VREDRAW;
  42.    wndclass.lpfnWndProc    = DemoWndProc;
  43.    wndclass.cbClsExtra     = 0;
  44.    wndclass.cbWndExtra     = 0;
  45.    wndclass.hInstance      = hInstance;
  46.    wndclass.hIcon          = LoadIcon(NULL,IDI_APPLICATION);
  47.    wndclass.hCursor        = LoadCursor( NULL, IDC_ARROW );
  48.    wndclass.hbrBackground  = (HBRUSH)GetStockObject( WHITE_BRUSH );
  49.    wndclass.lpszMenuName   = "DemoMenu";
  50.    wndclass.lpszClassName  = (LPSTR)szAppName;
  51.  
  52.    if (!RegisterClass(&wndclass))
  53.        return FALSE;
  54.    }
  55.  
  56.    hWnd = CreateWindow((LPSTR)szAppName,(LPSTR)szMessage,
  57.                         WS_OVERLAPPEDWINDOW,
  58.                         CW_USEDEFAULT,0,CW_USEDEFAULT,0,NULL,NULL,
  59.                         hInstance,NULL);
  60.  
  61.    hInst = hInstance;
  62.  
  63.    ShowWindow(hWnd,cmdShow);
  64.    UpdateWindow(hWnd);
  65.  
  66.    while (GetMessage(&msg,NULL,0,0))
  67.       {
  68.       TranslateMessage(&msg);
  69.       DispatchMessage(&msg);
  70.       }
  71.  
  72.    return (int)msg.wParam;
  73. }
  74.  
  75. /************************************************************************/
  76. long FAR PASCAL DemoWndProc(hWnd,message,wParam,lParam)
  77. HWND hWnd;
  78. unsigned message;
  79. WORD wParam;
  80. LONG lParam;
  81. {
  82.  
  83.     switch (message)
  84.     {
  85.  
  86.     case WM_DESTROY:
  87.         PostQuitMessage(0);
  88.         break;
  89.  
  90.     case WM_COMMAND:
  91.         switch(wParam)
  92.            {
  93.             case ID_PRINT:
  94.                Print(hWnd);
  95.                break;
  96.  
  97.             default:
  98.                return DefWindowProc(hWnd,message,wParam,lParam );
  99.                break;
  100.            }
  101.          break;
  102.  
  103.     case WM_PAINT:
  104.         DemoPaint(hWnd);
  105.         break;
  106.  
  107.     default:
  108.         return DefWindowProc( hWnd, message, wParam, lParam );
  109.         break;
  110.     }
  111.     return(0L);
  112. }
  113.  
  114. /************************************************************************/
  115. VOID Print(hWnd)
  116. HWND hWnd;
  117. {
  118. HDC hPrinterDC;
  119. char szDefaultDevice[80];
  120. PSTR spDefaultDeviceName,spPort,spFileName;
  121. int iLength;
  122. HCURSOR hOldCursor ;
  123.  
  124. hOldCursor = SetCursor (LoadCursor (NULL, IDC_WAIT)) ;
  125.  
  126. /* Use the default printer. */
  127. iLength = GetProfileString("windows","device",(LPSTR)"",szDefaultDevice,80);
  128. spDefaultDeviceName = strtok(szDefaultDevice,",");
  129. spFileName = strtok(NULL,", ");
  130. spPort = strtok(NULL,", ");
  131.  
  132. /* Get a display context. */
  133. hPrinterDC = CreateDC((LPSTR)spFileName,(LPSTR)spDefaultDeviceName,
  134.                       (LPSTR)spPort,(LPSTR)NULL);
  135.  
  136. Escape(hPrinterDC,STARTDOC,4,"HPCLIP",(LPSTR)NULL);
  137.  
  138. SetMapMode(hPrinterDC,MM_LOENGLISH);
  139.  
  140. DrawFilledArea(hWnd,hPrinterDC);
  141.  
  142. Escape(hPrinterDC,NEWFRAME,0,(LPSTR)NULL,(LPSTR)NULL);
  143. Escape(hPrinterDC,ENDDOC,NULL,(LPSTR)NULL,(LPSTR)NULL);
  144.  
  145. DeleteDC(hPrinterDC);
  146.  
  147. SetCursor (hOldCursor) ;
  148. MessageBeep (0) ;
  149. }
  150.  
  151. /************************************************************************/
  152. /*            DRAW FILLED AREA                */
  153. /************************************************************************/
  154.  
  155. VOID DrawFilledArea(hWnd,hDC)
  156. HWND hWnd;
  157. HDC hDC;
  158. {
  159. HRGN hFrontRgn;
  160. FARPROC lpProcAddress;
  161. RECT rcRect;
  162. POINT pt;
  163. HPEN hOldPen ;
  164. HBRUSH hOldBrush ;
  165. char szStr[20] ;
  166.  
  167. TextOut(hDC,50,-50,"A circle filled with lines should appear below. ",48);
  168.  
  169. /* Form the area in logical coordinates. */
  170. rcRect.left = 100;
  171. rcRect.top = -100;
  172. rcRect.right = 200;
  173. rcRect.bottom = -200;
  174.  
  175. SelectObject (hDC, GetStockObject (NULL_BRUSH)) ;
  176. /* Convert these to device coordinates. */
  177. LPtoDP(hDC,(LPPOINT)&rcRect,2);
  178.  
  179.  
  180. /* adjust the coordinates using the scaling factors */
  181.  
  182. pt.x = pt.y = 0;
  183. Escape(hDC,GETSCALINGFACTOR,0,NULL,(LPSTR)&pt);
  184. rcRect.left >>= pt.x;
  185. rcRect.right >>= pt.x;
  186. rcRect.top >>= pt.y;
  187. rcRect.bottom >>= pt.y;
  188.  
  189. // Create an elliptical region to fill in with lines.
  190. hFrontRgn = CreateEllipticRgn(rcRect.left,rcRect.top,
  191.                     rcRect.right,rcRect.bottom);
  192.  
  193. // Put this clipping region into the display context.
  194. SelectClipRgn(hDC,hFrontRgn);
  195.  
  196. // We pass the global variable to the call back procedure ShadeXProc.
  197. hShadeDC = hDC;
  198.  
  199. // Make the call back function.
  200. lpProcAddress = MakeProcInstance(ShadeXProc,hInst);
  201.  
  202. ptShadeLength.x = 100;
  203.  
  204. // We draw horizontal lines to fill in the area. Works on plotters and printers
  205. LineDDA(100,-100,100, -200,(FARPROC)lpProcAddress,(LPSTR)NULL);
  206.  
  207. FreeProcInstance(lpProcAddress);
  208. DeleteObject (hFrontRgn) ;
  209.  
  210. }
  211.  
  212. /************************************************************************/
  213. /*            DEMO PAINT                    */
  214. /************************************************************************/
  215.  
  216. VOID DemoPaint(hWnd)
  217. HWND hWnd;
  218. {
  219. HDC hDC;
  220. PAINTSTRUCT ps;
  221.  
  222. hDC = BeginPaint(hWnd,&ps);
  223. SetMapMode(hDC,MM_LOENGLISH);
  224.  
  225. DrawFilledArea(hWnd,hDC);
  226.  
  227. EndPaint(hWnd,&ps);
  228. }
  229.  
  230. /************************************************************************/
  231. /*            SHADE X PROC                    */
  232. /************************************************************************/
  233.  
  234. /*
  235. **  This module draws a horizontal line for each point it is given.
  236. */
  237.  
  238. VOID FAR PASCAL ShadeXProc(x,y,lpData)
  239. short x;
  240. short y;
  241. LPSTR lpData;
  242. {
  243. MoveTo(hShadeDC,x,y);
  244. LineTo(hShadeDC,x+ptShadeLength.x,y);
  245. }
  246.  
  247. 
  248.